// 学生接口定义
export interface Student {
  id: number;
  studentId: string;
  name: string;
  gender: string;
  age: number;
  major: string;
  class: string;
  admissionYear: string;
  phone: string;
  email: string;
  address: string;
  status: string;
  marketingDepartment: string;
  dormitory?: string;
  birthDate?: string;
  idCardNumber?: string;
  college?: string;
  // 添加成绩相关字段
  scoreDetails?: ScoreDetails;
}

// 成绩详情接口
export interface ScoreDetails {
  // 基本信息字段
  teachingCycle: string; // 教学周期
  type: string; // 类型
  educationLevel: string; // 学段
  counselor: string; // 导员
  lecturer: string; // 讲师
  teacherId: string; // 教师号
  teachingProgress: string; // 教学进度
  course: string; // 课程
  teacherInCharge: string; // 任课教师
  
  // 统计数据字段（放在详情中）
  totalStudents: number; // 总人数
  notCounted: number; // 不计数
  failed: number; // 不及格
  directPromotion: number; // 直升人数
  promotionRate: number; // 升班率
  averageScore: number; // 平均分
  examSubjects: string[]; // 考试科目
  writtenExamRatio: number; // 笔试占比
  computerExamRatio: number; // 机试占比
  interviewRatio: number; // 面试占比
  
  // 成绩列表
  scoreList: ScoreItem[];
}

// 成绩项接口
export interface ScoreItem {
  subject: string; // 科目
  score: number; // 分数
  examType: string; // 考试类型
  examDate: string; // 考试日期
  rank: number; // 排名
}

// 模拟学生数据
export let mockStudents: Student[] = [
  {
    id: 1,
    studentId: '2023001',
    name: '张三',
    gender: '男',
    age: 19,
    major: '计算机科学与技术',
    class: '2412A',
    admissionYear: '2023',
    phone: '13800138001',
    email: 'zhangsan@example.com',
    address: '北京市朝阳区',
    status: '在读',
    marketingDepartment: '省外市场部',
    dormitory: '男生1号楼-101',
    birthDate: '2004-05-15',
    idCardNumber: '110101200405151234',
    college: '计算机科学与技术学院',
    scoreDetails: {
      teachingCycle: '2023-2024学年第一学期',
      type: '期中考试',
      educationLevel: '本科',
      counselor: '李老师',
      lecturer: '王教授',
      teacherId: 'T2023001',
      teachingProgress: '已完成70%',
      course: '高等数学',
      teacherInCharge: '张教授',
      totalStudents: 45,
      notCounted: 2,
      failed: 5,
      directPromotion: 8,
      promotionRate: 85,
      averageScore: 78.5,
      examSubjects: ['高等数学', '线性代数', '概率论'],
      writtenExamRatio: 60,
      computerExamRatio: 30,
      interviewRatio: 10,
      scoreList: [
        {
          subject: '高等数学',
          score: 85,
          examType: '笔试',
          examDate: '2023-11-15',
          rank: 15
        },
        {
          subject: '线性代数',
          score: 92,
          examType: '笔试',
          examDate: '2023-11-18',
          rank: 8
        },
        {
          subject: '概率论',
          score: 78,
          examType: '笔试',
          examDate: '2023-11-20',
          rank: 22
        }
      ]
    }
  },
  {
    id: 2,
    studentId: '2023002',
    name: '李四',
    gender: '女',
    age: 18,
    major: '车辆工程',
    class: '2411B',
    admissionYear: '2023',
    phone: '13800138002',
    email: 'lisi@example.com',
    address: '上海市浦东新区',
    status: '在读',
    marketingDepartment: '省外市场部',
    dormitory: '女生2号楼-203',
    birthDate: '2005-02-18',
    idCardNumber: '310101200502185678',
    college: '机械工程学院',
    scoreDetails: {
      teachingCycle: '2023-2024学年第一学期',
      type: '期中考试',
      educationLevel: '本科',
      counselor: '赵老师',
      lecturer: '刘教授',
      teacherId: 'T2023002',
      teachingProgress: '已完成65%',
      course: '大学物理',
      teacherInCharge: '周教授',
      totalStudents: 38,
      notCounted: 1,
      failed: 3,
      directPromotion: 6,
      promotionRate: 90,
      averageScore: 82.3,
      examSubjects: ['大学物理', '材料力学', '理论力学'],
      writtenExamRatio: 70,
      computerExamRatio: 20,
      interviewRatio: 10,
      scoreList: [
        {
          subject: '大学物理',
          score: 90,
          examType: '笔试',
          examDate: '2023-11-16',
          rank: 5
        },
        {
          subject: '材料力学',
          score: 85,
          examType: '笔试',
          examDate: '2023-11-19',
          rank: 10
        },
        {
          subject: '理论力学',
          score: 88,
          examType: '笔试',
          examDate: '2023-11-21',
          rank: 7
        }
      ]
    }
  }
];

// 获取所有学生数据
export const getStudents = async (): Promise<Student[]> => {
  // 模拟API延迟
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(mockStudents);
    }, 300);
  });
};

// 添加学生
export const addStudent = async (student: Omit<Student, 'id'>): Promise<Student> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const newStudent: Student = {
        ...student,
        id: Math.max(...mockStudents.map(s => s.id)) + 1
      };
      mockStudents.push(newStudent);
      resolve(newStudent);
    }, 300);
  });
};

// 更新学生信息
export const updateStudent = async (id: number, updatedData: Partial<Student>): Promise<Student | null> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const index = mockStudents.findIndex(s => s.id === id);
      if (index !== -1) {
        mockStudents[index] = { ...mockStudents[index], ...updatedData };
        resolve(mockStudents[index]);
      } else {
        resolve(null);
      }
    }, 300);
  });
};

// 删除学生
export const deleteStudent = async (id: number): Promise<boolean> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const initialLength = mockStudents.length;
      mockStudents = mockStudents.filter(s => s.id !== id);
      resolve(mockStudents.length < initialLength);
    }, 300);
  });
};